//
//  SettingViewController.m
//  ACSProduct
//
//  Created by preet dhillon on 13/12/11.
//  Copyright (c) 2011 dhillon. All rights reserved.
// london W11 2BQ

#import "SettingViewController.h"

@implementation SettingViewController
@synthesize stringPhoneLocation,arrayDistance;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


-(IBAction)clickForMilesCriteria:(id)sender
{
    pickerDis.hidden = NO;
}

-(void)pressDoneForSel:(NSString *)stringSel withindex:(NSInteger)indexRow
{
    pickerDis.hidden = YES;
    //    radius = [[arrayDistanceFilter objectAtIndex:indexRow ]intValue];
    //    if(switchpostCode.on)
    
//    self.stringPhoneLocation = [arrayDistanceFilter objectAtIndex:indexRow ];
//    [ModalController  saveTheContent:self.stringPhoneLocation
//                             withKey:sPhoneLocation]; 
//    
//    fieldText.text = stringSel;
    
//    NSLog(@"----[[arrayDistanceFilter objectAtIndex:indexRow ]intValue]--------%d",[[arrayDistanceFilter objectAtIndex:indexRow ]intValue]);

//    switch (selctedBtn.tag) {
//        case 0:
//        {
//            NSMutableArray *arrayDis = [[NSMutableArray alloc] initWithArray:arrayDistance];
//            [pickerDis addTheElement:arrayDis];
//            pickerDis.hidden = NO;
//        }
//            break;
//        case 1:
//        {
//            NSMutableArray *arrayDis = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithArray:[KCITIES componentsSeparatedByString:@";"]]];
//            [pickerDis addTheElement:arrayDis];
//            pickerDis.hidden = NO;
//        }
//            break;
//            
//        default:
//            break;
//    }

    if(selctedBtn.tag == 0)
    {
        self.stringPhoneLocation = [arrayDistanceFilter objectAtIndex:indexRow ];
        [ModalController  saveTheContent:self.stringPhoneLocation
                             withKey:sPhoneLocation];
        [[NSNotificationCenter  defaultCenter] postNotificationName:[arrayReloadingFactor  objectAtIndex:0]
                                                             object:nil];
         [selctedBtn setTitle:stringSel forState:UIControlStateNormal];
    }
    else
    {
        [selctedBtn setTitle:stringSel
                    forState:UIControlStateNormal];
        [ModalController  saveTheContent:stringSel
                                 withKey:[arraySectionTtle  objectAtIndex:selctedBtn.tag]];
    }
  
}


-(IBAction)switchChangedPost:(id)sender
{
//    [[NSNotificationCenter  defaultCenter] postNotificationName:KsReloadDATA object:nil];
 
    if(switchpostCode.on)
        [ModalController saveTheContent:@"y"
                                withKey:RADSAVEDSAT];
    else
    {
        [ModalController saveTheContent:@"n"
                                withKey:RADSAVEDSAT];
    }
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    if(KISPOSTCODE)
    {
    arrayReloadingFactor = [[NSMutableArray  alloc] initWithObjects:KsReLOADDATAMETERRAN,KsReLOADDATAPlace,KsReLOADDATAZipCode,KsReLOADDATAHOME, nil];
    arraySwitchedLocation = [[NSMutableArray  alloc] initWithObjects:KsSWITCHEDMETER,ksSWITCHEDPLACE,KsSWITCHEDPOSTCODE,ksSWITCHEDHOME, nil];
    
    arraySectionTtle = [[NSMutableArray alloc] initWithObjects:@"Phone Location",@"Place",@"Postcode",@"Home",nil];
    
    }
    else
    {
        arrayReloadingFactor = [[NSMutableArray  alloc] initWithObjects:KsReLOADDATAMETERRAN,KsReLOADDATAPlace,KsReLOADDATAZipCode,KsReLOADDATAHOME, nil];
        arraySwitchedLocation = [[NSMutableArray  alloc] initWithObjects:KsSWITCHEDMETER,ksSWITCHEDPLACE,KsSWITCHEDPOSTCODE,ksSWITCHEDHOME, nil];
        
        arraySectionTtle = [[NSMutableArray alloc] initWithObjects:@"Phone Location",@"Place",nil];
        
        [ModalController  removeContentForKey:KsSWITCHEDPOSTCODE];
        [ModalController  removeContentForKey:ksSWITCHEDHOME];
        
        [ModalController  removeContentForKey:@"Postcode"];
        [ModalController  removeContentForKey:@"Home"];
    }
    
    
//    if([[ModalController  getContforKey:RADSAVEDSAT] isEqualToString:@"y"])
//        [switchpostCode setOn:YES];
    
    self.arrayDistance = [NSArray arrayWithObjects:@"0-1",@"0-3",@"0-5",@">5", nil];
    arrayDistanceFilter = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"1",@"3",@"5",@"20000", nil]];
    
    dicFilterLoc = [[NSMutableDictionary  alloc] init];
    for(int i=0;i<[arrayDistanceFilter  count];i++)
        [dicFilterLoc setObject:[self.arrayDistance  objectAtIndex:i] forKey:[arrayDistanceFilter  objectAtIndex:i]];
    
    
    NSMutableArray *arrayDis = [[NSMutableArray alloc] initWithArray:arrayDistance];
    
    pickerDis = [[ViewPickerTool alloc] initWithFrame:CGRectMake(0, 0, 320, 194)];
    [self.view addSubview:pickerDis];
    [pickerDis setClient:self];
    pickerDis.hidden = YES;
    [pickerDis addTheElement:arrayDis];
    
    
    [self setLocation];
    
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)setLocation
{
    if(![ModalController  getContforKey:@"Phone Location"])
        [ModalController  saveTheContent:@"20000" withKey:@"Phone Location"];
    
    if(![ModalController  getContforKey:[arraySwitchedLocation  objectAtIndex:0]])
        [ModalController  saveTheContent:@"y" withKey:[arraySwitchedLocation  objectAtIndex:0]];

    
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -

#pragma mark - UITableView delegates

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	// The number of sections is based on the number of items in the data property list.
	return [arraySectionTtle  count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewHeader=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    viewHeader.backgroundColor=[UIColor clearColor];
    UILabel *lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 320, 20)];
    lblTitle.text = [arraySectionTtle  objectAtIndex:section];
    lblTitle.font=[UIFont boldSystemFontOfSize:15.0];
    lblTitle.textColor=[UIColor lightGrayColor];
    lblTitle.backgroundColor=[UIColor clearColor];;
    [viewHeader addSubview:lblTitle];
    return viewHeader;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{	
	//return [arrayList count];	
    return 1;	
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{	
	
	SettingCell *cell = (SettingCell *)[tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
	if(cell == nil) 
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingCell" owner:self options:nil];
        cell = (SettingCell *)[nib objectAtIndex:0];
    }
    
    cell.delegateCell=self;
    cell.btnSetting.tag= indexPath.section;
    
//    [cell  setViews];
//    [cell.txtFldCell setTag:indexPath.section];
//    cell.txtFldCell.textColor=[UIColor redColor];
//    cell.txtFldCell.font=[UIFont systemFontOfSize:12.0];
    cell.swtchCell.tag=indexPath.section;
//
//    
    if(indexPath.section == 0)
    {
//        fieldText = cell.txtFldCell;
        if([ModalController  getContforKey:[arraySectionTtle  objectAtIndex:indexPath.section]])
            [ cell.btnSetting setTitle:[dicFilterLoc  objectForKey:[ModalController  getContforKey:[arraySectionTtle  objectAtIndex:indexPath.section]]] forState:UIControlStateNormal];
    }
    else
        [cell.btnSetting setTitle:[ModalController  getContforKey:[arraySectionTtle  objectAtIndex:indexPath.section]] forState:UIControlStateNormal];
    
    if([ModalController  getContforKey:[arraySwitchedLocation  objectAtIndex:indexPath.section]] >0)
        [cell.swtchCell setOn:YES];
    else
        [cell.swtchCell setOn:NO];
    
//    if([ModalController  getContforKey:[arraySectionTtle  objectAtIndex:indexPath.section]] >0)
//        [cell.swtchCell setOn:YES];
//    else
//        [cell.swtchCell setOn:NO];
    
    
//
//    if(indexPath.section == 2)
//        cell.txtFldCell.placeholder = @"Press to enter Place";
//    else if(indexPath.section == 1)
//        cell.txtFldCell.placeholder = @"Press to enter Post Code";
//    else if(indexPath.section == 3)
//        cell.txtFldCell.placeholder = @"Press to enter Home Post Code";
//    
//    
//    return (UITableViewCell *)cell;
    return (UITableViewCell *)cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
}



#pragma mark-Switch cell delegate


-(void)clickToSwitchWithTag:(NSInteger)tag withString:(NSString *)fieldvalue;
{

    NSLog(@"%@",fieldvalue);
    
//    if(tag==0)
//    {
//        [ModalController  saveTheContent:self.stringPhoneLocation
//                                 withKey:[arraySectionTtle  objectAtIndex:tag]];
//    }
//    else
//        [ModalController  saveTheContent:fieldvalue
//                                 withKey:[arraySectionTtle  objectAtIndex:tag]];
    

    [ModalController  saveTheContent:@"y" 
                             withKey:[arraySwitchedLocation  objectAtIndex:tag]];
    
    [[NSNotificationCenter  defaultCenter] postNotificationName:[arrayReloadingFactor  objectAtIndex:tag]
                                                          object:nil];
}


-(void)clickToBtn:(id)sender
{
    selctedBtn = (UIButton *)sender;
    
    switch (selctedBtn.tag) {
        case 0:
        {
            NSMutableArray *arrayDis = [[NSMutableArray alloc] initWithArray:arrayDistance];
            [pickerDis addTheElement:arrayDis];
            pickerDis.hidden = NO;
        }
        break;
        case 1:
        {
            NSMutableArray *arrayDis = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithArray:[KCITIES componentsSeparatedByString:@";"]]];
            [pickerDis addTheElement:arrayDis];
            pickerDis.hidden = NO;
        }
            break;
        case 2:
        {
           UIAlertView * createNewAlert =[[UIAlertView alloc] initWithTitle:@"Please fill it" message:@"Put it blank textfield will cover this" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
            createNewAlert.tag = 1233;
            fieldText = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
//            fieldText.text=@"";
            [fieldText setBackgroundColor:[UIColor whiteColor]];
            [fieldText setKeyboardAppearance:UIKeyboardAppearanceAlert];
            [fieldText setAutocorrectionType:UITextAutocorrectionTypeNo];
            
            [fieldText setTextAlignment:UITextAlignmentCenter];
            [createNewAlert addSubview:fieldText];
            
            
            [createNewAlert show];
        }
            break;
        case 3:
        {
            UIAlertView * createNewAlert =[[UIAlertView alloc] initWithTitle:@"Please fill it" message:@"Put it blank textfield will cover this" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
            createNewAlert.tag = 1233;
            fieldText = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
//            fieldText.text=@"";
            [fieldText setBackgroundColor:[UIColor whiteColor]];
            [fieldText setKeyboardAppearance:UIKeyboardAppearanceAlert];
            [fieldText setAutocorrectionType:UITextAutocorrectionTypeNo];
            
            [fieldText setTextAlignment:UITextAlignmentCenter];
            [createNewAlert addSubview:fieldText];
            [createNewAlert show];
        }
            break;
    
        default:
            break;
    }

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 1233 && buttonIndex == 1)
    {
        [selctedBtn setTitle:fieldText.text
                    forState:UIControlStateNormal];
        [ModalController  saveTheContent:fieldText.text
                                 withKey:[arraySectionTtle  objectAtIndex:selctedBtn.tag]];

    }
}


-(void)removeTheContent:(NSInteger)tag
{
    [ModalController   removeContentForKey:[arraySwitchedLocation  objectAtIndex:tag]];
    [[NSNotificationCenter  defaultCenter] postNotificationName:[arrayReloadingFactor  objectAtIndex:tag] object:nil];
}


-(void)showPicker
{
    pickerDis.hidden = NO;
}

#pragma mark -removeTheContent-


//-(void)clickToSwitch:(id)sender
//{
//    NSLog(@"click btn cell=%d",[sender tag]);
//}
//
//-(void)downkeyPadWithvalue:(NSString *)value withTag:(NSInteger)tag
//{
//    NSLog(@"%s",__PRETTY_FUNCTION__);
//    if([value length]>0)
//        [ModalController  saveTheContent:value
//                             withKey:[arraySectionTtle  objectAtIndex:tag]];
//    else
//        [ModalController  removeContentForKey:[arraySectionTtle  objectAtIndex:tag]];
//    
//    [tblViewSetting setContentOffset:CGPointMake(0.0, 0.0)
//                            animated:YES];
//}
//
//-(void)settextfieldFrame:(UITextField *)field
//{
//    [tblViewSetting setContentOffset:CGPointMake(0.0, 70.0*field.tag)
//                            animated:YES];    
//}
//

@end
